/** 
 * Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package at.socialconference.web.client.controller;

import com.google.gwt.user.client.rpc.AsyncCallback;

import at.socialconference.inter.model.User;
import at.socialconference.web.client.SCwebService;
import at.socialconference.web.client.ui.AdminHome;
import at.socialconference.web.client.ui.FriendsHome;
import at.socialconference.web.client.ui.FriendsSearch;
import at.socialconference.web.client.ui.FriendsTwitter;
import at.socialconference.web.client.ui.Home;
import at.socialconference.web.client.ui.Login;
import at.socialconference.web.client.ui.Registration;
import at.socialconference.web.client.ui.SCwebView;
import at.socialconference.web.client.util.AsyncCallbackGetClientUser;

/**
 * Controlles the view and offers methods to update it
 * 
 */
public class ViewController {

	private class AsyncCallbackExistsValidSession implements AsyncCallback<Boolean>  {
		@Override
		public void onFailure(Throwable caught) {}
		@Override
		public void onSuccess(Boolean result) {
			if (result.equals(true)) {
				SCwebService.Util.getInstance().getClientUser(new AsyncCallbackGetClientUser());
			}
			else {
				changeViewToLoggedOut();
			}
		}
	}
	
	private static ViewController instance;

	private SCwebView view = new SCwebView();
	private User user;
	
	private ViewController() {
		SCwebService.Util.getInstance().existsValidSession(new AsyncCallbackExistsValidSession());
	}

	/**
	 * Returns the current instance of the ViewController
	 * 
	 * @return
	 */
	public static ViewController getInstance() {
		if (instance == null)
			instance = new ViewController();
		return instance;
	}

	/**
	 * Returns the view controlled by the ViewController
	 * 
	 * @return
	 */
	public SCwebView getView() {
		return view;
	}

	/**
	 * Shows the registration-form in the middle of the root DockLayoutPanel
	 */
	public void showRegisterForm() {
		view.updateMiddle(new Registration());
	}

	/**
	 * Shows the login-form in the middle of the root DockLayoutPanel
	 */
	public void showLoginForm() {
		view.updateMiddle(new Login());
	}

	/**
	 * Changes the status of the view from logged out to logged in.
	 */
	public void changeViewToLoggedIn() {
		view.updateMenu(true);
		showClientHome();
	}

	/**
	 * Changes the status of the view from logged in to logged out.
	 */
	public void changeViewToLoggedOut() {
		view.updateMiddle(new Login());
		view.updateMenu(false);
	}

	/**
	 * Returns the user of the client
	 * 
	 * @return
	 */
	public User getClientUser() {
		return user;
	}

	/**
	 * Sets the user of the client
	 * 
	 * @param user
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * Shows the FriendsHome
	 */
	public void showFriendsHome() {
		view.updateMiddle(new FriendsHome());
	}

	/**
	 * Shows the FriendsTwitter
	 */
	public void showFriendsTwitter() {
		view.updateMiddle(new FriendsTwitter());
	}
	
	/**
	 * Shows Admininistration Area
	 */
	public void showAdminHome() {
		view.updateMiddle(new AdminHome());
	}

	/**
	 * Shows the FriendsSearch
	 */
	public void showFriendsSearch() {
		view.updateMiddle(new FriendsSearch());
	}

	/**
	 * Shows the Home view of the client
	 */
	public void showClientHome() {
		view.updateMiddle(new Home());
	}
}
